package edu.pku.sei.modeler.figures;


import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

public class UseInterfaceFigure extends Figure {
	private Point sourceLocation = null;
	private Point targetLocation = null;
	public UseInterfaceFigure(){
	}
	public void paintFigure(Graphics g){
		g.pushState();
		int startArc = this.getStartArc();
		int endArc = this.toEndArc(startArc);
		g.setLineWidth(2);
		g.drawArc(this.bounds.x + 2, this.bounds.y + 2, this.bounds.width - 4, this.bounds.height - 4, startArc, 180);
		g.popState();
	}
	private int getStartArc(){
		int sourceX;
		int sourceY;
		
		if (sourceLocation == null){
			sourceX = this.bounds.x + this.bounds.width / 2 + 10;
			sourceY = this.bounds.y + this.bounds.height / 2;
		} else {
			sourceX = this.sourceLocation.x;
			sourceY = this.sourceLocation.y;
		}
		int startAngle;
		int targetX = this.targetLocation.x + this.bounds.width / 2;
		int targetY = this.targetLocation.y + this.bounds.height / 2;
		
		double bevelEdge = Math.pow(Math.pow(sourceX - targetX, 2) + Math.pow(sourceY - targetY, 2), 0.5);
		if (sourceX <= targetX){
			double a = targetY - sourceY;
			startAngle = (int) (Math.acos(a/bevelEdge)*180/Math.PI);
		} else {
			double a = sourceY - targetY;
			startAngle = 180 + (int) (Math.acos(a/bevelEdge)*180/Math.PI);
		}
		return startAngle;
	}
	private int toEndArc(int startArc){
		if(startArc + 180 < 360)
			return startArc + 180;
		else return startArc - 180;
	}
	public void setBounds(Rectangle rect){
		super.setBounds(rect);
	}
	public void setLocation(Point source, Point target) {
		this.sourceLocation = source;
		this.targetLocation = target;
	}
}
